<template>
  <div class="Cropper" v-show="cropperImg">
    <div class="cropper-container">
      <div class="cropper-el">
        <vue-cropper
          v-bind="$attrs"
          ref="cropper"
          :img="cropperImg"
          :output-size="option.size"
          :output-type="option.outputType"
          :info="true"
          :full="option.full"
          :fixed="fixed"
          :fixed-number="fixedNumber"
          :can-move="option.canMove"
          :can-move-box="option.canMoveBox"
          :fixed-box="option.fixedBox"
          :original="option.original"
          :auto-crop="option.autoCrop"
          :auto-crop-width="optionWidth"
          :auto-crop-height="optionHeigth"
          :center-box="option.centerBox"
          @real-time="realTime"
          :high="option.high"
          @img-load="imgLoad"
          mode="cover"
          :max-img-size="option.max"
        ></vue-cropper>
      </div>
      <div class="bottom">
        <a-button @click="clearfinish">
          取消裁剪
        </a-button>
        <a-button type="primary" @click="finish">
          确定裁剪
        </a-button>
      </div>
    </div>
  </div>
</template>

<script>
import { VueCropper } from 'vue-cropper'
export default {
  name: 'Cropper',
  components: {
    VueCropper
  },
  props: {
    cropperImg: {
      type: String
    },
    optionWidth: {
      // 裁剪框宽
      default: 900
    },
    optionHeigth: {
      // 裁剪高宽
      default: 600
    },
    fixedNumber: {
      default: () => [1.5, 1]
    }
  },
  data() {
    return {
      previews: {},
      option: {
        img: this.cropperImg,
        size: 1, // 裁剪生成图片的质量
        full: false,
        outputType: 'png', // 裁剪生成图片的格式 默认jpg
        canMove: true, // 上传图片是否可以移动
        fixedBox: false,
        original: false,
        canMoveBox: true,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 900,
        autoCropHeight: 600,
        centerBox: true,
        high: true,
        max: 99999
      },
      show: true,
      fixed: true, // 是否开启截图框宽高固定比例 (默认:true)
      // fixedNumber: [1.5, 1], // 截图框的宽高比例,
      modelSrc: ''
    }
  },
  methods: {
    // 裁剪时触发的方法，用于实时预览
    realTime(data) {
      this.previews = data
    },
    // 图片加载的回调 imgLoad 返回结果success, error
    imgLoad(e) {
      if (e === 'success') {
        console.log('裁剪成功')
      }
    },
    // 确定裁剪
    finish() {
      this.$refs.cropper.getCropBlob((data) => {
        let img = window.URL.createObjectURL(data)
        this.modelSrc = img
        const files = new window.File([data], Date.parse(new Date()) + '.png', { type: data.type })
        files.url = window.URL.createObjectURL(data)
        this.$emit('getCrofile', files)
      })
    },
    clearfinish() {
      this.$emit('clear')
    }
  }
}
</script>

<style lang="scss" scoped>
.Cropper {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999999;
  background-color: #fff;
  .cropper-el {
    width: 70vw;
    height: 70vh;
    margin: 30px auto;
  }
  .bottom {
    width: 100%;
    text-align: center;
  }
}
</style>
